package org.wzl.personalwebsite.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 卫志龙
 * @version 1.0
 * @date 2024/9/22 10:08
 */
@Slf4j
public class JsonWrapperUtil {

    private static ObjectMapper objectMapper = new ObjectMapper();

    public static <T> T toObject(String json, TypeReference<T> type) {
        return objectMapper.convertValue(json, type);
    }

    public static String toJson(Object obj) {
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            log.error("序列化失败", e);
            return "";
        }
    }

}
